{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-26T02:04:13.524018Z",
     "start_time": "2019-08-26T02:04:09.177638Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TIANCHENGRONGZU\t1451\t0.5858\t0.6376\t128\t0.6172\t0.5891\n",
      "TIANCHENGRONGZU\t710\t0.762\t0.7324\t26\t0.9231\t0.7321\n",
      "TIANCHENGRONGZU\t630\t0.7222\t0.7712\t25\t0.84\t0.7857\n",
      "TIANCHENGRONGZU\t3123\t0.6033\t0.7977\t119\t0.6218\t0.7782\n",
      "TIANCHENGRONGZU\t3681\t0.6172\t0.6443\t216\t0.6019\t0.6575\n",
      "TIANCHENGRONGZU\t2163\t0.6893\t0.7462\t130\t0.7308\t0.7303\n",
      "TIANCHENGRONGZU\t2141\t0.8085\t0.7666\t124\t0.7903\t0.7719\n",
      "TIANCHENGRONGZU\t2505\t0.8232\t0.6528\t74\t0.8649\t0.6848\n",
      "TIANCHENGRONGZU\t2830\t0.8365\t0.7462\t135\t0.8444\t0.7857\n",
      "TIANCHENGRONGZU\t706\t0.7762\t0.656\t24\t0.9167\t0.7381\n",
      "TIANCHENGRONGZU\t2087\t0.6608\t0.6988\t108\t0.6389\t0.7093\n",
      "TIANCHENGRONGZU\t1094\t0.8729\t0.7047\t76\t0.9079\t0.6875\n",
      "TIANCHENGRONGZU\t70\t0.7714\t0.7285\t4\t0.5\t0.6667\n",
      "TIANCHENGRONGZU\t243\t0.823\t0.7157\t15\t0.9333\t0.75\n",
      "TIANCHENGRONGZU\t2102\t0.7755\t0.7175\t118\t0.822\t0.6418\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<os._wrap_close at 0x1a86b5c9cf8>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 识别前处理\n",
    "# 图片二值化\n",
    "from PIL import Image\n",
    "import os\n",
    "os.chdir('D:\\OCR')\n",
    "img = Image.open('3.png')\n",
    " \n",
    "# 模式L”为灰色图像，它的每个像素用8个bit表示，0表示黑，255表示白，其他数字表示不同的灰度。\n",
    "Img = img.convert('L')\n",
    "Img.save(\"test1.png\")\n",
    " \n",
    "# 自定义灰度界限，大于这个值为黑色，小于这个值为白色\n",
    "threshold = 200\n",
    " \n",
    "table = []\n",
    "for i in range(256):\n",
    "    if i < threshold:\n",
    "        table.append(0)\n",
    "    else:\n",
    "        table.append(1)\n",
    " \n",
    "# 图片二值化\n",
    "photo = Img.point(table, '1')\n",
    "photo.save(\"test2.png\")\n",
    "# 识别图片内容\n",
    "import pytesseract\n",
    "img_path = 'test2.png'\n",
    "\n",
    "text=pytesseract.image_to_string(Image.open(img_path))\n",
    "text = text.replace(' ','\\t')\n",
    "with open('text.txt','w+') as f:\n",
    "    f.write(text)\n",
    "\n",
    "print(text)\n",
    "os.popen(r'C:\\Program Files (x86)\\Notepad++\\notepad++')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
